package com.ubao.network;

import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import android.content.Context;

import com.ubao.activity.MyApp;
import com.ubao.util.MD5;
import com.ubao.util.Util;
import com.ubao.util.log.Logger;

public class Url {
	public static final String PARAM = "PARAM";
	public static final String SUCCESS = "200";
	
    public static final String url = "106.3.45.41";
    
    public static final String url_register = "/account/register";
    public static final String url_register_full = url + url_register;
    
    public static final String url_productadd = "/product/add";
    public static final String url_productadd_full = url + url_productadd;
    
    public static final String url_login = "/account/login";
    public static final String url_login_full = url + url_login;
    
    public static final String url_categorylist = "/category/list";
    public static final String url_categorylist_full = url + url_categorylist;
    
    
    public static final String url_friendshipsadd = "/friendships/add";
    public static final String url_friendshipsadd_full = url + url_friendshipsadd;
    
    public static final String url_friendshipsdelete = "/friendships/delete";
    public static final String url_friendshipsdelete_full = url + url_friendshipsdelete;
    
    public static final String url_friendshipsfollowinglist = "/friendships/followinglist";
    public static final String url_friendshipsfollowinglist_full = url + url_friendshipsfollowinglist;
    public static String getFriendshipsfollowinglist(Context ctx,String userid,String pagesize,String pageindex){
    	Map<String, String> para = new HashMap<String, String>();
    	para.put("userid", userid);
    	para.put("pagesize", pagesize);
    	para.put("pageindex", pageindex);
    	return getParams(url_friendshipsfollowinglist,para);
    }
    
    public static final String url_friendshipsfollowerslist = "/friendships/followerslist";
    public static final String url_friendshipsfollowerslist_full = url + url_friendshipsfollowerslist;
    public static final String url_smssend = "/sms/send";
    public static final String url_smssend_full = url + url_smssend;
    public static final String url_smsread = "/sms/read";
    public static final String url_smsread_full = url + url_smsread;
    public static final String url_smssendlist = "/sms/sendlist";
    public static final String url_smssendlist_full = url + url_smssendlist;
    public static final String url_smsreceivelist = "/sms/receivelist";
    public static final String url_smsreceivelist_full = url + url_smsreceivelist;
    
    public static final String url_commodityadd = "/commodity/add";
    public static final String url_commodityadd_full = url + url_commodityadd;
    /**
     * @param ctx
     * @param userid 参数：userid			string			账户的ID
     * @param title 参数：title			string			商品的标题
     * @param desc 参数：desc			string			商品的描述
     * @param typeid 参数：typeid			int				商品分类的ID
     * @param typename 参数：typename		string 			商品分类的名称
     * @param exchange 参数：exchange		int 			是否交换（0，为不交换 1，为交换）
     * @param longititude 参数：longitude		double 			经度
     * @param latitude 参数：latitude		double 			纬度
     * @param selftime 参数：shelftime		datetime		下架时间
     * @param releasetime 参数：releasetime   	datetime			上架时间
     * @return
     */
    public static Map<String,String> getCommodityadd(Context ctx,String userid,
    												String title,
    												String desc,
    												String typeid,
    												String typename,
    												String exchange,
    												String longitude,
    												String latitude,
    												String shelftime,
    												String releasetime
    												){
    	Map<String, String> para = new HashMap<String, String>();
    	para.put("userid", userid);
    	para.put("title", URLEncoder.encode(title));
    	para.put("desc", URLEncoder.encode(desc));
    	para.put("typeid", typeid);
    	para.put("typename", URLEncoder.encode(typename));
    	para.put("exchange", exchange);
    	para.put("longitude", longitude);
    	para.put("latitude", latitude);
    	para.put("shelftime", shelftime); 
    	para.put("releasetime", releasetime); 
    	return para;
    }
    
    public static final String url_commoditydelete = "/commodity/delete";
    public static final String url_commoditydelete_full = url + url_commoditydelete;
    
    public static final String url_commoditymylist = "/commodity/mylist";
    public static final String url_commoditymylist_full = url + url_commoditymylist;
    public static String getCommoditymylist(Context ctx,String userid,String pagesize,String pageindex){
    	Map<String, String> para = new HashMap<String, String>();
    	para.put("userid", userid);
    	para.put("pagesize", pagesize);
    	para.put("pageindex", pageindex);
    	return getParams(url_commoditymylist,para);
    }
    
    public static final String url_commoditylist = "/commodity/list";
    public static final String url_commoditylist_full = url + url_commoditylist;
    public static String getCommoditylist(Context ctx,String pagesize,String pageindex,String typeid,String distance,String longitude,String latitude){
    	Map<String, String> para = new HashMap<String, String>();
    	para.put("pagesize", pagesize);
    	para.put("pageindex", pageindex);
    	para.put("typeid", typeid);
    	para.put("distance", distance);
    	para.put("longitude", longitude);
    	para.put("latitude", latitude);
    	return getParams(url_commoditylist,para);
    }
    
    public static final String url_commoditydetail = "/commodity/detail";
    public static final String url_commoditydetail_full = url + url_commoditydetail;
    public static String getCommoditydetail(Context ctx,String commodityid,String longitude,String latitude){
    	Map<String, String> para = new HashMap<String, String>();
    	para.put("commodityid", commodityid);
    	para.put("longitude", longitude);
    	para.put("latitude", latitude);
    	return getParams(url_commoditydetail,para);
    }
    
    public static final String url_commodityaddstar = "/commodity/addstar";
    public static final String url_commodityaddstar_full = url + url_commodityaddstar;
    public static final String url_commentadd = "/comment/add";
    public static final String url_commentadd_full = url + url_commentadd;
    public static final String url_commentdelete = "/comment/delete";
    public static final String url_commentdelete_full = url + url_commentdelete;
    public static final String url_commentlist = "/comment/list";
    public static final String url_commentlist_full = url + url_commentlist;
    public static final String url_commodityfollowing = "/commodity/following";
    public static final String url_commodityfollowing_full = url + url_commodityfollowing;
    public static final String url_commodityunfollowing = "/commodity/unfollowing";
    public static final String url_commodityunfollowing_full = url + url_commodityunfollowing;
    public static final String url_commodityfollowinglist = "/commodity/followinglist";
    public static final String url_commodityfollowinglist_full = url + url_commodityfollowinglist;
    
    
    
    
    //123456-9999999
    public static String getNounce(){
    	double number = Math.random()*10000000;
    	if(number<123456){
    		number = number + 123456;
    	}
    	return String.valueOf((int)number);
    }
    
    public static String getTimeStamp(){
    	return String.valueOf(System.currentTimeMillis()/1000);
    }
    
    public static final String AUTH_KEY = "EE50108B814B4AF8B46BEEE3CD49D81E";
    public static final String AUTH_VERSION = "v1.0";
    public static final String AUTH_SECRET = "422ABAEF2C5446528B01D659A2F4A2F0";
    public static final String POST = "POST";
    
    public static final String getAuthParam(String urlabs,String tsp,String nounce){
    	String str = "POST&" + urlabs + "&auth_key=" + AUTH_KEY + "&auth_secret="+AUTH_SECRET+"&auth_nonce="+nounce+"&auth_timestamp="+tsp+"&auth_version=" + AUTH_VERSION;
    	String md5 = MD5.toMD5(str);
    	return md5;
    }
    
    public static final String getregisterParams(String userid,String password,String username,String mobile){
    	String tsp = Url.getTimeStamp(); 
		String nounce = Url.getNounce();
    	String paramStr = 
    	"userid=" + userid + "&password=" + password + "&username=" + username + "&mobile=" + mobile + "&auth_method=" 
		+ Url.POST + "&auth_key=" + Url.AUTH_KEY + "&auth_nonce=" + nounce + "&auth_timestamp=" + tsp
		+ "&auth_version=" + Url.AUTH_VERSION + "&auth_signature="+Url.getAuthParam(Url.url_register,tsp,nounce);
    	return paramStr;
    }
    
    public static final String getParams(String url,Map params){
    	String tsp = Url.getTimeStamp(); 
		String nounce = Url.getNounce();
		String paramStr = "";
		Object[] keys = params.keySet().toArray();
		for(int i=0;i<keys.length;i++){
			paramStr = paramStr + (String)keys[i] + "=" + (String)params.get(keys[i]) + "&";
		}
		paramStr = paramStr + "auth_method=" 
		+ Url.POST + "&auth_key=" + Url.AUTH_KEY + "&auth_nonce=" + nounce + "&auth_timestamp=" + tsp
		+ "&auth_version=" + Url.AUTH_VERSION + "&auth_signature="+Url.getAuthParam(url,tsp,nounce);
		Logger.d("paramStr:" + paramStr);
		return paramStr;
    }
    
    //http://code.google.com/p/panoramagl-android/
    
    
}
